<?php


namespace app\adminapi\logic\user;


use app\common\model\user\OpenvpnDept;
use app\common\model\user\Openvpn;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * openvpn控制器逻辑
 * Class OpenvpnLogic
 * @package app\adminapi\logic\user
 */
class OpenvpnLogic extends BaseLogic
{


    /**
     * @notes 添加openvpn控制器
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Openvpn::create([
                'sn' => $params['sn'],
                'common_name' => $params['common_name'],
                'user_name' => $params['user_name'],
                'vpn_ip' => $params['vpn_ip'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑openvpn控制器
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public static function edit(array $params, $adminId): bool
    {
        Db::startTrans();
        try {
            $vpnIp = Openvpn::where('id', $params['id'])->value('vpn_ip');
            Openvpn::where('id', $params['id'])->update([
                'sn' => $params['sn'],
                'common_name' => $params['common_name'],
                'user_name' => $params['user_name'],
                'vpn_ip' => $params['vpn_ip']
            ]);
//            修改vpn ip
            if ($vpnIp != $params['vpn_ip']) {
                $ifconfig = "ifconfig-push {$params['vpn_ip']} 255.255.255.0";
                @file_put_contents('/etc/openvpn/ccd/' . $params['common_name'], $ifconfig);
            }
            Db::commit();

            // 删除旧的关联信息
            if ($adminId == 1) {
                OpenvpnDept::delByVpnId($params['id']);
                self::insertDept($params['id'], $params['dept_id'] ?? []);
            }
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除openvpn控制器
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public static function delete(array $params): bool
    {
        return Openvpn::destroy($params['id']);
    }


    /**
     * @notes 获取openvpn控制器详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public static function detail($params): array
    {
        return Openvpn::findOrEmpty($params['id'])->toArray();
    }

    public static function insertDept($vpnId, $deptIds)
    {
        // 部门
        if (!empty($deptIds)) {
            $deptData = [];
            foreach ($deptIds as $deptId) {
                $deptData[] = [
                    'vpn_id' => $vpnId,
                    'dept_id' => $deptId
                ];
            }
            (new OpenvpnDept())->saveAll($deptData);
        }
    }
}